package com.car.controller.order;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.entity.Order;
import com.car.service.order.OrderService;
import com.car.service.order.OrderServiceImpl;
import com.car.util.ResultMsg;
import lombok.SneakyThrows;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: OrderController
 * @Description:
 * @Author 余全盛
 * @Date: 2021/12/2 13:57
 */
@Controller
public class OrderController {
    @Autowired
    private OrderService orderService;

    @RequestMapping("/toOrder")
    public String toOrder() {
        return "order-create";
    }

    @RequestMapping("/toOrderList")
    public String toOrderList() {
        return "order-list";
    }

    @SneakyThrows
    @RequestMapping("/createOrder")
    @ResponseBody
    public String createOrder(int customerId,
                              int startDepartmentId,
                              int endDepartmentId,
                              int orderCarId,
                              String orderReserveStartDate,
                              String orderReserveEndDate,
                              Double orderStartCarOilValue,
                              Double orderStartCarElectriclValue,
                              Integer orderState,
                              Double orderDeposit,
                              int orderDepositState,
                              Double orderOilPrice,
                              Double orderElectricPrice){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Order order = new Order();
        order.setCustomerId(customerId);
        order.setStartDepartmentId(startDepartmentId);
        order.setOrderCarId(orderCarId);
        order.setEndDepartmentId(endDepartmentId);
        order.setOrderReserveStartDate(sdf.parse(orderReserveStartDate));
        order.setOrderReserveEndDate(sdf.parse(orderReserveEndDate));
        order.setOrderStartCarOilValue(orderStartCarOilValue);
        order.setOrderStartCarElectriclValue(orderStartCarElectriclValue);
        order.setOrderState(orderState);
        order.setOrderDeposit(orderDeposit);
        order.setOrderDepositState(orderDepositState);
        order.setOrderOilPrice(orderOilPrice);
        order.setOrderElectricPrice(orderElectricPrice);
        int result = orderService.creatOrder(order);
        return result==1?"创建订单成功":"创建订单时出现异常";
    }

    @RequestMapping("/getOrders")
    @ResponseBody
    public Map<String, Object> getOrders(
            @RequestParam(required = false, defaultValue = "") String orderId,
            @RequestParam(required = false, defaultValue = "") String customerId,
            @RequestParam int draw,
            @RequestParam int start,
            @RequestParam int length) {
        Map<String, Object> param = new HashMap();
        int pageNum = (start / length) + 1;
        int pageSize = length;
        IPage<Order> page = new Page<>(pageNum, pageSize);
        param.put("orderId", orderId);
        param.put("customerId", customerId);
        param.put("page", page);
        List<Order> orderList = orderService.getOrders(param);
        Map<String, Object> map = new HashMap();
        map.put("draw", draw);
        map.put("data", orderList);
        map.put("recordsTotal", page.getTotal());
        map.put("recordsFiltered", page.getTotal());
        return map;
    }

    @RequestMapping("/updateOrderState")
    @ResponseBody
    public String updateOrderState(String orderId, int orderState) {
        return orderService.updateOrderState(orderId, orderState) == 1 ? "更改订单信息成功" : "更改订单信息出现异常";
    }

    @RequestMapping("/updateOrderInfo")
    @ResponseBody
    public String updateOrderInfo(String orderId, int orderState, Double orderEndCarOilValue, Double orderEndCarElectriclValue) {
        Date orderEndDate = new Date();
        return orderService.updateOrderInfo(orderId, orderState, orderEndCarOilValue, orderEndCarElectriclValue, orderEndDate) == 1 ? "更改订单信息成功" : "更改订单信息出现异常";
    }



}
